﻿<%@ WebHandler Language="C#" Class="SIPO.Web.AjaxHandler.Transaction.recRestructurDetailhandler" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

using SIPO.Library.DataAccess.Transaction;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Administration;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;

using SIPO.Library.Enum;
using SIPO.Web.Support;

using Newtonsoft.Json.Linq;
using System.Web.SessionState;

namespace SIPO.Web.AjaxHandler.Transaction
{
    public class recRestructurDetailhandler : SIPO.Web.BaseHandler, IRequiresSessionState
    {
        private recRestructurDetailRepo repo =
                new recRestructurDetailRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        private recDemandRepo repoDemand =
            new recDemandRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        private recLogRepo logDal = 
            new recLogRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        private string MethodName = string.Empty;
        
        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);
            
            MethodName = context.Request.Params["method"];
            
            context.Response.Clear();
            context.Response.Buffer = true;

            System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream);

            string jsonString = string.Empty;
            jsonString = context.Server.UrlDecode(sr.ReadToEnd());
            JObject jObj = string.IsNullOrEmpty(jsonString) ? null : JObject.Parse(jsonString);

            sr.Close();
            sr.Dispose();

            switch (MethodName.ToLower())
            {
                case ("getdatabyresid"):
                    context.Response.Write(GetDataByRestructurID(context, jObj));
                    break;
                default:
                    // do nothing
                    break;
            }
        }
        
        private string GetDataByRestructurID(HttpContext context, JObject jo)
        {
            int page = 1;
            //int.TryParse(context.Request.QueryString["page"] ?? "1", out page);

            int pageSize = 10;
            //int.TryParse(context.Request.QueryString["rows"] ?? "10", out pageSize);

            //string sidx = ((context.Request.QueryString["sidx"]) == null ? recRestructurDetail.Fields.RestructurDetailID : context.Request.QueryString["sidx"].ToString());
            //string sord = ((context.Request.QueryString["sord"]) == null ? "ASC" : context.Request.QueryString["sord"].ToString());

            List<recRestructurDetail> pagedList = null;
            List<recRestructurDetailDTO> recRestructurDetailDTOs = null;
            try
            {
                List<recRestructur> rec = (List<recRestructur>)context.Session["punyaseno"];

                pagedList = rec[int.Parse(context.Request.QueryString["rowid"].ToString()) - 1].recRestructurDetails;

                recRestructurDetailDTOs =  
                pagedList.Select(x => new recRestructurDetailDTO
                { 
                    IDTransaksi = x.TransNo,
                    JenisPungutan = x.TransTypeName,
                    Nilai = x.TransValues,
                    NoSanksi = x.SanctionNo
                }).ToList<recRestructurDetailDTO>();
            }
            catch (Exception ex)
            {
                recLog log = new recLog();
                log.LogEvent = "GetDataByRestructurID";
                log.LogMessage = ex.Message;
                log.LogSource = "recRestructurDetailhandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
                log.Dispose();
            }
            finally
            {

            }
            
            // send params into jqGrid's format
            var gridData = new
            {
                page = page,
                total = pageSize,
                records = recRestructurDetailDTOs.Count,
                rows = recRestructurDetailDTOs
            };

            return Newtonsoft.Json.JsonConvert.SerializeObject(gridData);
        }
    }
}